package com.me.utils;

import static android.text.Spanned.SPAN_EXCLUSIVE_INCLUSIVE;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.Resources;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.TextView;

import com.me.bean.Xt;
import com.me.bean.Xy;
import com.me.blood.App;
import com.me.blood.R;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Utils {

    public static int getStatusBarHeight(Context c) {
        int result = 0;
        Resources resources = c.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = resources.getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public static int dp2Px(Context context, float dipValue) {
        final float scale = context.getResources()
                .getDisplayMetrics().density;
        int pxValue = (int) (dipValue * scale + 0.5f);

        return pxValue;
    }

    public static int getScreenWidth() {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) App.getApp()
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
                .getMetrics(dm);
        return dm.widthPixels;
    }

    public static int getScreenHeight() {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) App.getApp()
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
                .getMetrics(dm);
        return dm.heightPixels;
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / (scale <= 0 ? 1 : scale) + 0.5f);
    }

    public static void freeWeb(WebView wb) {
        try {
            if (wb == null) {
                return;
            }
            if (wb.getParent() instanceof ViewGroup) {
                ((ViewGroup) wb.getParent()).removeView(wb);
            }
            wb.setWebChromeClient(null);
            wb.setWebViewClient(null);
            wb.removeAllViews();
            wb.destroy();
        } catch (Throwable e) {

        }
    }

    public static void setClickableText(TextView tv, String str) {
        int color = isTwo() ? 0xff00B386 : 0xff38BFC6;
        SpannableString ss = new SpannableString(str);
        MyClickableSpan s = new MyClickableSpan(tv.getContext(), Constant.AGREE, color);
        int i1 = str.indexOf("《");
        int e1 = str.indexOf("》") + 1;
        ss.setSpan(s, i1, e1, SPAN_EXCLUSIVE_INCLUSIVE);
        int i2 = str.indexOf("《", i1 + 1);
        int e2 = str.indexOf("》", e1) + 1;
        ss.setSpan(new MyClickableSpan(tv.getContext(), Constant.PRIVACY, color), i2, e2, SPAN_EXCLUSIVE_INCLUSIVE);
        tv.setLineSpacing(0, 1.2f);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        tv.setHighlightColor(tv.getContext().getResources().getColor(android.R.color.transparent));
        tv.setText(ss);
    }

    public final static boolean isTwo() {
        return com.me.blood.BuildConfig.APPLICATION_ID.endsWith(".me");
    }

    public static String getDay(long time) {
        Date d = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String day = sdf.format(d);
        return day;
    }

    public static String getHour(long time) {
        Date d = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String day = sdf.format(d);
        return day;
    }

    public static long date2Ms(String _data) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        try {
            Date date = format.parse(_data);
            return date.getTime();
        } catch (Exception e) {
            return 0;
        }
    }

    public static String getState(Xt item) {
        if (item.value < 3.9) {
            return "偏低";
        } else if (item.value > 6.5) {
            return "偏高";
        }
        return "正常";
    }

    public static int getStateRes(Xt item) {
        String s = getState(item);
        if ("正常".equals(s)) {
            return R.drawable.green_round;
        } else if ("偏高".equals(s)) {
            return R.drawable.orange_round;
        } else {
            return R.drawable.blue_round;
        }
    }

    public static int getStateColor(Xt item) {
        String s = getState(item);
        if ("正常".equals(s)) {
            return 0xff73DD5F;
        } else if ("偏高".equals(s)) {
            return 0xffFF763B;
        } else {
            return 0xff45C6FF;
        }
    }

    public static String getValue(float value) {
        DecimalFormat decimalFormat = new DecimalFormat("#0.0"); // 设置格式化模式为保留两位小数
        String result = decimalFormat.format(value);
        return result;
    }

    public static String getValue(Xy value) {
        return value.high + "/" + value.low;
    }

    public static String getState(Xy item, int type) {
        if (type == 1) {
            if (item.high > 130) {
                return "高压偏高";
            } else if (item.high < 90) {
                return "高压偏低";
            }
            return "高压正常";
        } else if (type == 2) {
            if (item.low > 80) {
                return "低压偏高";
            } else if (item.low < 60) {
                return "低压偏低";
            }
            return "低压正常";
        } else {
            if (item.high >= 90 && item.high <= 130 && item.low >= 60 && item.low <= 80) {
                return "正常";
            }
        }
        return "正常";
    }

    public static int getStateColor(Xy item, int type) {
        String s = getState(item, type);
        if (s.contains("正常")) {
            return 0xff73DD5F;
        } else if (s.contains("高")) {
            return 0xffFF763B;
        } else {
            return 0xff45C6FF;
        }
    }

    public static int getStateRes(Xy item) {
        String s = getState(item, 1);
        if (s.contains("正常")) {
            return R.drawable.green_round;
        } else if (s.contains("高")) {
            return R.drawable.orange_round;
        } else {
            return R.drawable.blue_round;
        }
    }

    public final static void copy(String str) {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) App.getApp().getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", str);
// 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
    }
}
